package anonymouschat.client.frames;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;

import anonymouschat.Identity;
import anonymouschat.client.Client;
import anonymouschat.utils.ConstantVariables;
import anonymouschat.utils.LocalFileOps;

/**
 * ID selection GUI. By selecting persist or new identity, default chat room and
 * nick name list are displayed.
 * 
 * @author hb
 * 
 */
public class IDFrame extends JFrame implements ActionListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    Client client;

    Identity lastIdentity;

    final int WIDTH = 500;

    final int HEIGHT = 500;

    JButton[] buttons;

    /**
     * Create a two button UI where user can either persist as their last id or
     * Generate a new one
     */
    public IDFrame( final Client client ) {
        this.client = client;
        setTitle( "Welcome to Anonymous Chat System" );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( WIDTH, HEIGHT );
        setVisible( true );
        buttons = new JButton[2];
        for( int i = 0; i < 2; i++ ) {
            buttons[i] = new JButton();
        }
        try {
            lastIdentity = LocalFileOps.readLastIdentity();
        } catch( IOException e ) {
            e.printStackTrace();
        } catch( ClassNotFoundException e ) {
            e.printStackTrace();
        }
        if( lastIdentity == null ) {
            buttons[0].setEnabled( false );
        }
        buttons[0].setText( "Persist on last Identity: " + lastIdentity );
        buttons[0].addActionListener( this );
        buttons[1].setText( "Use new Identity" );
        buttons[1].addActionListener( this );
        setLayout( new GridLayout( 2, 1 ) );
        for( int i = 0; i < 2; i++ ) {
            add( buttons[i] );
        }
        validate();
    }

    @Override
    public void actionPerformed( ActionEvent e ) {
        String s = e.getActionCommand();
        if( buttons[0].getText().equals( s ) ) {
            client.setUserID( lastIdentity );
        } else {
            Identity newIdentity = new Identity( "newID"
                    + (int) (Math.random() * 100) );
            client.setUserID( newIdentity );
            try {
                LocalFileOps.updateLastIdentity( newIdentity );
            } catch( FileNotFoundException e1 ) {
                e1.printStackTrace();
            } catch( IOException e1 ) {
                e1.printStackTrace();
            }
            LocalFileOps.removeAllRecords();
        }
        client.setNickListFrame( new NickListFrame( client ) );
        client.setChatFrame( new ChatFrame( client,
                ConstantVariables.DEFAULT_CHATROOM_NAME ) );
        dispose();

    }

}
